package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.OmBatchDeleteRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmpPositionRequest;
import org.tis.tools.abf.module.om.entity.OmEmpPosition;
import org.tis.tools.abf.module.om.service.IOmEmpPositionService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * omEmpPosition的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omEmpPosition")
@Slf4j
public class OmEmpPositionController extends BaseController<OmEmpPosition>  {

    @Autowired
    private IOmEmpPositionService omEmpPositionService;


    @ApiOperation(value = "新增岗位员工",notes = "传入OmEmpPositionRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增岗位员工")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmEmpPositionRequest",name = "传入的OmEmpPositionRequest类",required = true) OmEmpPositionRequest omEmpPositionRequest) {
        omEmpPositionService.add(omEmpPositionRequest);
        return ResultVO.success("新增成功！");
    }

    @ApiOperation(value = "修改岗位员工",notes = "传入OmEmpPosition类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改岗位员工")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "OmEmpPosition",name = "传入的OmEmpPosition类",required = true) OmEmpPosition omEmpPosition) {
        OmEmpPosition omEmpPositionQue = omEmpPositionService.selectById(omEmpPosition.getGuid());
        if (omEmpPositionQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omEmpPositionService.updateById(omEmpPosition);
        return ResultVO.success("修改成功！",omEmpPosition);
    }


    @ApiOperation(value = "根据ID删除岗位员工",notes = "传入岗位员工的GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据ID删除岗位员工")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "岗位员工的GUID",required = true) String id) {
        OmEmpPosition omEmpPositionQue = omEmpPositionService.selectById(id);
        if (omEmpPositionQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omEmpPositionService.deleteById(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据员工和岗位ID删除机构下的岗位员工",notes = "传入员工的GUID(guidEmp)和岗位的GUID(guidPosition)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据员工和岗位ID删除机构下的岗位员工")
    @DeleteMapping("/delete/{guidEmp}/{guidPosition}")
    public ResultVO deleteByEmpPositionId(@PathVariable @NotBlank(message = "员工GUID不能为空") @ApiParam(value = "guidEmp",name = "员工的GUID",required = true) String guidEmp,@PathVariable @NotBlank(message = "所在岗位GUID不能为空") @ApiParam(value = "guidPosition",name = "岗位的GUID",required = true) String guidPosition) {
        omEmpPositionService.deleteByEmpPositionId(guidEmp,guidPosition);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "根据员工和岗位ID批量删除机构下的岗位员工",notes = "传入员工的GUID(guidEmp)和岗位的GUID(guidPosition)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据员工和岗位ID批量删除机构下的岗位员工")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入的OmBatchDeleteRequest类",required = true) OmBatchDeleteRequest request) {
        omEmpPositionService.batchDelete(request.getGuid(),request.getDeleteList());
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据员工和岗位ID删除工作组下的岗位员工",notes = "传入员工的GUID(guidEmp)和岗位的GUID(guidPosition)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据员工和岗位ID删除工作组下的岗位员工")
    @DeleteMapping("/{guidEmp}/deleteByGroup/{guidPosition}")
    public ResultVO deleteByGroup(@PathVariable @NotBlank(message = "员工GUID不能为空") @ApiParam(value = "guidEmp",name = "员工的GUID",required = true) String guidEmp,@PathVariable @NotBlank(message = "所在岗位GUID不能为空") @ApiParam(value = "guidPosition",name = "岗位的GUID",required = true) String guidPosition) {
        omEmpPositionService.deleteByEmpPositionId(guidEmp,guidPosition);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "根据员工和岗位ID批量删除工作组下的岗位员工",notes = "传入员工的GUID(guidEmp)和岗位的GUID(guidPosition)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据员工和岗位ID批量删除工作组下的岗位员工")
    @PostMapping("/batchDeleteByGroup")
    public ResultVO batchDeleteByGroup(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入的OmBatchDeleteRequest类",required = true) OmBatchDeleteRequest request) {
        omEmpPositionService.batchDelete(request.getGuid(),request.getDeleteList());
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询岗位员工",notes = "传入岗位员工的GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "岗位员工的GUID",required = true) String id) {
        OmEmpPosition omEmpPosition = omEmpPositionService.selectById(id);
        if (omEmpPosition == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omEmpPosition);
    }


    @ApiOperation(value = "分页查询岗位员工",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息",required = true) SmartPage<OmEmpPosition> page) {
        return  ResultVO.success("查询成功", omEmpPositionService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }
    
}

